// 地址栏参数初始值
import {getHttpPath} from '../../js/my-utils'
import {_TOTAL_PATH_} from '../../js/my-core'

export const defaultPath = (_this) => {
  const arr = ['?sort=0&page=1', '?sort=0&page=1', '?sort=0&page=1']
  return _this.$route.path + arr[_this.urlPage]
}
// 初始化地址栏参数
export const initQuery = (_this) => {
  // 初始化 pagination.currentPage
  if (_this.$route.query['page'] === undefined) {
    _this.$router.push(defaultPath(_this))
    return
  }
  if (_this.$route.query['sort'] === undefined) {
    _this.$router.push(defaultPath(_this))
    return
  }
  const page = _this.$route.query['page']
  if (page > _this.pagination.totalPage || page < 1) {
    _this.$router.push(defaultPath(_this))
    return
  }
  const sort = _this.$route.query['sort']
  if (sort < 0 || page > 9) {
    _this.$router.push(defaultPath(_this))
    return
  }
  // 分页当前页码
  _this.pagination.currentPage = page
  // 地址栏参数
  _this.data.page = page
  _this.data.sort = sort
  initAnime(_this)
}
// 番剧数据初始化
export const initAnime = (_this) => {
  if (_this.sortList[_this.sortSelected].child) {
    _this.data.sort = _this.sortSelected * 2
  } else {
    _this.data.sort = _this.sortSelected * 2 + 1
  }
  let path = _this.$route.path + '?'
  path += 'sort=' + _this.data.sort + '&'
  _this.pagination.path = path
  path += 'page=' + _this.data.page
  return path
}
// 从后端获取数据
export const getAllList = (_this) => {
  const url = ['/api/anime/all', '/api/manga/all', '/api/novel/all']
  _this.$axios({
    method: 'post',
    url: url[_this.urlPage],
    data: _this.$qs.stringify(_this.data)
  })
    .then((res) => {
      if (res.data.code === 200) {
        const total = Number(res.data.data.total)
        const shang = parseInt((total / 20).toString())
        _this.pagination.totalPage = total % 20 === 0 ? shang : shang + 1
        _this.pagination.totalNum = total
        _this.allVideoList = []
        for (let item of res.data.data.list) {
          let video = {id: item.id, data: {cover: null, href: '#', name: 'NULL', tag: 2, status: 0, allNum: 0, refreshNum: 0}}
          video.data.cover = getHttpPath(item.cover)
          video.data.href = _TOTAL_PATH_[_this.urlPage] + item.id
          video.data.name = item.name
          video.data.tag = item.tag
          video.data.status = item.status
          video.data.allNum = item.allNum
          video.data.refreshNum = item.refreshNum
          _this.allVideoList.push(video)
        }
        // console.log(res.data.data.list)
      } else {
        console.log(res.data.msg)
      }
    })
    .catch((error) => {
      console.log(error)
    })
}
